<?php
/**
 * ExtObservableBase class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */
 
 
abstract class ExtObservableBase extends ExtWidget
{
    private $_events = array();
    private $_actions_class_types = array('ExtClientAction','ExtServerAction');
    private $_default_event_action = 'ext4yii.eventAutoResult(true)';

    public function parseNode(ExtHTMLParserNode $node,
        ExtASTWalker $astwalker)
    {
        parent::parseNode($node,$astwalker);
        $this->parseActions($node, $astwalker);
    }


    private function parseActions($node,$astwalker)
    {
        $actions = $node->findPropertyByName('actions');
        if(!is_null($actions))
        {
            $items=parent::parseNodeWidthTag($actions,
                $astwalker,
                true,
                null,
                $this,
                $this->_actions_class_types);
            $actions->shiftUp();
        }
    }

    public function addEvent($name,$type,$sig,$function)
    {
        if(!isset($this->_events[$name]))
            $this->_events[$name] = array(
                'sig' => $sig,
                'client' => null,
                'server' => null);

        $this->_events[$name][$type] = $function;
    }

    public function renderConfig($asobject=true)
    {
        $listeners = array();
        if(count($this->_events) != 0)
        {
            foreach($this->_events as $name => $event)
            {
                if(is_null($event['client']))
                    $client_event = $this->_default_event_action;
                else
                    $client_event = sprintf('this.%s(%s)',$event['client'],$event['sig']);

                if(is_null($event['server']))
                    $server_event = $this->_default_event_action;
                else
                    $server_event = sprintf('this.%s(%s)',$event['server'],$event['sig']);

                $function = sprintf('function(%s) { if(ext4yii.eventAutoResult(%s)) { %s; } else { return false; } } ',
                    $event['sig'],
                    $client_event,
                    $server_event);

                $listeners[$name] = $this->JSImply($function);
            }
            $this->setObjectConfig('listeners', $listeners);
        }
        return parent::renderConfig($asobject);
    }
}
?>
